package com.zerdoor.service.impl;

import com.zerdoor.dao.FundManageDao;
import com.zerdoor.service.FundManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 资金管理 业务层接口实现类
 * Created by zhongjieyu on 2015/1/29.
 */
@Service
public class FundManageServiceImpl implements FundManageService{

    @Autowired
    private FundManageDao fundManageDao;

    @Override
    public List<Map> findUserWithdraw(Map param) throws Exception {


        return fundManageDao.findUserWithdraw(param);
    }

    @Override
    public void updateWithdrawStatus(Map<String, Object> result) throws Exception {

        int withdrawStatus = (Integer)result.get("withdrawStatus");

        int withdrawId = (Integer)result.get("withdrawId");

        Map withdrawMap = fundManageDao.findWithdrawInfoById(withdrawId);

        if(withdrawMap == null){

            new Exception("提现操作失败，提现记录ID为空或状态不为待审批！提现记录ID：" + withdrawId);
        }

        // 钱包记录map
        Map<String,Object> walletMap = new HashMap<String, Object>();

        //封装日志信息
        walletMap.put("money",withdrawMap.get("money") );
        walletMap.put("type", 112106 );
        walletMap.put("user_id", withdrawMap.get("user_id") );

        //130103 表示成功
        if(withdrawStatus == 130103){

            //扣除金额
            fundManageDao.updateUserWalletById(withdrawMap);

            //成功状态
            walletMap.put("wallet_status", "131101");
            walletMap.put("remark", "提现成功！提现记录ID：" + withdrawId + "，操作员ID：" + result.get("userId") + "。");


        }else{

            //解冻金额
            fundManageDao.updateThawFreezeMoneyByUserId(withdrawMap);

            //失败状态
            walletMap.put("wallet_status", "131102");
            walletMap.put("remark", "提现失败！提现记录ID：" + withdrawId + "，操作员ID：" + result.get("userId") + "，失败原因：被打回。" );

        }

        fundManageDao.addUser_wallet(walletMap);
        fundManageDao.updateWithdrawStatus(result);
    }

}
